package com.example.seven.mynews.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.seven.mynews.adapter.MyPageAdapter;
import com.example.seven.mynews.R;
import com.example.seven.mynews.fragment.Flagment1;
import com.example.seven.mynews.fragment.Flagment2;
import com.example.seven.mynews.fragment.Flagment3;
import com.example.seven.mynews.fragment.Flagment4;
import com.example.seven.mynews.fragment.Flagment5;
import com.example.seven.mynews.fragment.Flagment6;
import com.example.seven.mynews.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by seven on 16-6-29.
 */
public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private NavigationView navigationView;
    private TextView loginName;
    private boolean hasLogin=false;
    private SharedPreferences sharedPreferences;
//    private RecyclerView recyclerView;
//    private List<String> mData;
//    private SimpleAdapter simpleAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //ToolBar + drawerLayout
        initToobarDrawer();

        //TabLayout + ViewPage
        initTabView();

        //NavigationListener
        initNav();

        //判断是否已经登陆
        hasLogin();

//        //抓取数据
//        getData();

//        //recyclerView
//        initRecycler();

    }


    private void initToobarDrawer() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawLayout);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.nav_open,R.string.nav_close);
        actionBarDrawerToggle.syncState();
        drawerLayout.addDrawerListener(actionBarDrawerToggle);

    }

    private void initTabView() {
        List<Fragment> fragments = new ArrayList<>();

        fragments.add(Flagment1.newInstance("国内新闻"));
        fragments.add(Flagment2.newInstance("互联网"));
        fragments.add(Flagment3.newInstance("人物志"));
        fragments.add(Flagment4.newInstance("趋势报告"));
        fragments.add(Flagment5.newInstance("IT业界"));
        fragments.add(Flagment6.newInstance("实战讲堂"));
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.mainViwePage);

        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        //tab的模式如果标签多的话用MODE_SCROLLABLE  少的话用MODE_FIXED
        MyPageAdapter pageAdapter = new MyPageAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void initNav() {
        navigationView = (NavigationView) findViewById(R.id.navigation);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id = item.getItemId();
                switch (id) {
                    case R.id.nav_home:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;
                    case R.id.nav_group:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;
                    case R.id.nav_favorite:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;
                    case R.id.nav_help:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;
                    case R.id.nav_account:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;
                    case R.id.nav_settings:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;
                    case R.id.nav_more:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;

                    case R.id.nav_spellcheck:
                        item.setChecked(true);
                        setTitle(item.getTitle());
                        drawerLayout.closeDrawers();

                        break;
                }
                return true;
            }
        });
    }

    private void hasLogin() {

        loginName = (TextView) findViewById(R.id.loginName);
        sharedPreferences = getSharedPreferences("xy",MODE_PRIVATE);
        hasLogin = sharedPreferences.getBoolean("hasLogin",false);
        if (hasLogin){
            //    loginName.setVisibility(View.GONE);
        }else{
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("hasLogin",false);
            editor.commit();
        }
    }



//    private void initRecycler() {
//        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
//
//        mData = new ArrayList<>();
//        for (int i='A';i<'Z';i++){
//            mData.add(""+(char)i);
//        }
//
//    }

    public void checkLogin(View view){
        sharedPreferences = getSharedPreferences("xy", MODE_PRIVATE);
        hasLogin = sharedPreferences.getBoolean("hasLogin",true);
        if (hasLogin){

        }else {

            Intent aIntent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(aIntent);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.settings:

                break;
            case R.id.search:

                break;
            case R.id.share:

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
